package com.shiro.sso.demo.utils;

import com.shiro.sso.demo.config.properties.AuthProperties;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SsoLoginHelper {

    private static AuthProperties authProperties;
    public static String cookieName;

    @Autowired
    public void setSsoProperties(AuthProperties authProperties) {
        SsoLoginHelper.authProperties = authProperties;
        cookieName = authProperties.getCookie().getCookieName();
    }

    public static AuthProperties getShiroProperties() {
        return SsoLoginHelper.authProperties;
    }

    /**
     * get sessionid by cookie (web)
     */
    public static String getSessionIdByCookie(HttpServletRequest request) {
        String cookieSessionId = CookieUtil.getValue(request, cookieName);
        return cookieSessionId;
    }

    /**
     * set sessionid in cookie (web)
     */
    public static void setSessionIdInCookie(HttpServletResponse response, String sessionId) {
        if (sessionId != null && sessionId.trim().length() > 0) {
            CookieUtil.set(response, cookieName, sessionId, false);
        }
    }

    /**
     * remove sessionId in cookie (web)
     */
    public static void removeSessionIdInCookie(HttpServletRequest request,
            HttpServletResponse response) {
        CookieUtil.remove(request, response, cookieName);
    }

    /**
     * load cookie sessionid (app)
     */
    public static String cookieSessionIdGetByHeader(HttpServletRequest request) {
        String cookieSessionId = request.getHeader(cookieName);
        return cookieSessionId;
    }

    /**
     * login check
     */
    public static Session isLogin(HttpServletRequest request) {
        String cookieSessionId = getSessionIdByCookie(request);
        if (cookieSessionId != null && cookieSessionId.trim().length() > 0) {
            return isLogin(cookieSessionId);
        }
        return null;
    }

    /**
     * login check
     */
    public static Session isLogin(String sessionId) {
        return StringUtils.isNotBlank(sessionId) ? SsoLoginStore.get(sessionId) : null;
    }

    /**
     * client login (web)
     */
    public static void loginSuccess(HttpServletResponse response, Session session) {
        String sessionId = session.getId().toString();
        CookieUtil.set(response, cookieName, sessionId, false);
        SsoLoginStore.put(sessionId, session);
    }

    /**
     * client login (app)
     */
    public static void loginSuccess(String sessionId, Session session) {
        SsoLoginStore.put(sessionId, session);
    }


    /**
     * client logout (web)
     */
    public static void logout(HttpServletRequest request, HttpServletResponse response) {
        String cookieSessionId = getSessionIdByCookie(request);
        if (StringUtils.isNotBlank(cookieSessionId)) {
            SsoLoginStore.remove(cookieSessionId);
        }
        CookieUtil.remove(request, response, cookieName);
    }

    /**
     * client logout (app)
     */
    public static void logout(String sessionId) {
        SsoLoginStore.remove(sessionId);
    }

}
